// 示例数据，用于测试界面效果
// 在实际部署时，这些数据将从明道云API获取

export const mockInventoryData = [
  {
    id: '1',
    productImage: 'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=80&h=80&fit=crop',
    productName: '优质红富士苹果',
    storageCondition: '常温保存，避免阳光直射',
    supplier: '山东果品有限公司',
    barcode: '6901234567890',
    productionDate: '2024-01-15',
    availableStock: 150,
    lockedStock: 20,
    totalStock: 170,
    locationCode: 'A01-01-01'
  },
  {
    id: '2',
    productImage: 'https://images.unsplash.com/photo-1571771894821-ce9b6c11b08e?w=80&h=80&fit=crop',
    productName: '有机香蕉',
    storageCondition: '冷藏保存 2-8°C',
    supplier: '海南热带水果集团',
    barcode: '6901234567891',
    productionDate: '2024-01-20',
    availableStock: 80,
    lockedStock: 10,
    totalStock: 90,
    locationCode: 'B02-01-03'
  },
  {
    id: '3',
    productImage: 'https://images.unsplash.com/photo-1557800636-894a64c1696f?w=80&h=80&fit=crop',
    productName: '进口新奇士橙子',
    storageCondition: '常温保存',
    supplier: '进口水果贸易公司',
    barcode: '6901234567892',
    productionDate: '2024-01-10',
    availableStock: 200,
    lockedStock: 15,
    totalStock: 215,
    locationCode: 'C03-02-01'
  },
  {
    id: '4',
    productImage: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=80&h=80&fit=crop',
    productName: '精选芒果',
    storageCondition: '常温保存，成熟后冷藏',
    supplier: '广西热带果业',
    barcode: '6901234567893',
    productionDate: '2024-01-25',
    availableStock: 120,
    lockedStock: 8,
    totalStock: 128,
    locationCode: 'A01-02-01'
  },
  {
    id: '5',
    productImage: 'https://images.unsplash.com/photo-1559181567-c3190ca9959b?w=80&h=80&fit=crop',
    productName: '新鲜柠檬',
    storageCondition: '冷藏保存 4-8°C',
    supplier: '四川柑橘专业合作社',
    barcode: '6901234567894',
    productionDate: '2024-01-18',
    availableStock: 90,
    lockedStock: 5,
    totalStock: 95,
    locationCode: 'B02-03-02'
  },
  {
    id: '6',
    productImage: 'https://images.unsplash.com/photo-1587049352851-8d4e89133924?w=80&h=80&fit=crop',
    productName: '优质猕猴桃',
    storageCondition: '冷藏保存 0-2°C',
    supplier: '陕西奇异果种植基地',
    barcode: '6901234567895',
    productionDate: '2024-01-12',
    availableStock: 75,
    lockedStock: 12,
    totalStock: 87,
    locationCode: 'C03-01-01'
  },
  {
    id: '7',
    productImage: 'https://images.unsplash.com/photo-1613743983303-b3e89f8a2b80?w=80&h=80&fit=crop',
    productName: '精品龙眼',
    storageCondition: '常温保存，通风干燥',
    supplier: '福建龙眼种植合作社',
    barcode: '6901234567896',
    productionDate: '2024-01-22',
    availableStock: 60,
    lockedStock: 3,
    totalStock: 63,
    locationCode: 'A01-03-01'
  },
  {
    id: '8',
    productImage: 'https://images.unsplash.com/photo-1471157479190-c70a52dd4066?w=80&h=80&fit=crop',
    productName: '新鲜草莓',
    storageCondition: '冷藏保存 0-4°C',
    supplier: '大连草莓种植园',
    barcode: '6901234567897',
    productionDate: '2024-01-28',
    availableStock: 45,
    lockedStock: 7,
    totalStock: 52,
    locationCode: 'B02-02-01'
  },
  // 测试多供应商的相同条码商品
  {
    id: '9',
    productImage: 'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=80&h=80&fit=crop',
    productName: '特价苹果 A级',
    storageCondition: '常温保存',
    supplier: '北京果业集团',
    barcode: '690123', // 使用特殊条码测试多供应商
    productionDate: '2024-02-01',
    availableStock: 100,
    lockedStock: 0,
    totalStock: 100,
    locationCode: 'C01-01-01'
  },
  {
    id: '10',
    productImage: 'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=80&h=80&fit=crop',
    productName: '精品苹果 B级',
    storageCondition: '常温保存',
    supplier: '上海果品有限公司',
    barcode: '690123', // 相同条码，不同供应商
    productionDate: '2024-02-02',
    availableStock: 80,
    lockedStock: 5,
    totalStock: 85,
    locationCode: 'C01-02-01'
  },
  {
    id: '11',
    productImage: 'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=80&h=80&fit=crop',
    productName: '年货苹果 C级',
    storageCondition: '常温保存',
    supplier: '广东果业贸易',
    barcode: '690123', // 相同条码，第三个供应商
    productionDate: '2024-02-03',
    availableStock: 60,
    lockedStock: 10,
    totalStock: 70,
    locationCode: 'C01-03-01'
  }
];

// 智能搜索函数，支持多种搜索模式
export function smartFilterMockData(searchTerm) {
  if (!searchTerm || !searchTerm.trim()) return [];
  
  const trimmedTerm = searchTerm.trim();
  console.log('模拟数据搜索，搜索词:', trimmedTerm);
  
  // 导入搜索类型检测函数（模拟）
  const detectSearchType = (value) => {
    if (!value) return 'general';
    
    const val = value.trim();
    
    // 检测条码格式
    if (/^[A-Z0-9]{6,}$/i.test(val)) {
      if (/^[A-Z]\d+$|^[A-Z]-\d+-\d+$|^[A-Z]+\d+$/i.test(val)) {
        return 'location';
      }
      return 'barcode';
    }
    
    // 检测库位编码格式
    if (/^[A-Z]+\d*-?\d*-?\d*$/i.test(val) && val.length <= 15) {
      return 'location';
    }
    
    // 纯数字，可能是条码
    if (/^\d{8,}$/.test(val)) {
      return 'barcode';
    }
    
    return 'general';
  };
  
  const searchType = detectSearchType(trimmedTerm);
  console.log('检测到搜索类型:', searchType);
  
  return mockInventoryData.filter(item => {
    if (searchType === 'location') {
      // 库位编码精确匹配
      const itemLocation = item.locationCode || '';
      const isExactMatch = itemLocation.toLowerCase() === trimmedTerm.toLowerCase();
      console.log(`模拟数据库位编码比较:`);
      console.log(`  商品: ${item.productName}`);
      console.log(`  商品库位: "${itemLocation}"`);
      console.log(`  搜索库位: "${trimmedTerm}"`);
      console.log(`  匹配结果: ${isExactMatch}`);
      return isExactMatch;
    } else if (searchType === 'barcode') {
      // 商品条码精确匹配
      const isExactMatch = item.barcode === trimmedTerm;
      console.log(`条码比较: "${item.barcode}" === "${trimmedTerm}" -> ${isExactMatch}`);
      return isExactMatch;
    } else {
      // 通用搜索：商品名称模糊匹配 + 条码和库位编码精确匹配
      const nameMatch = item.productName.toLowerCase().includes(trimmedTerm.toLowerCase());
      const barcodeMatch = item.barcode === trimmedTerm;
      const locationMatch = item.locationCode.toLowerCase() === trimmedTerm.toLowerCase();
      
      const result = nameMatch || barcodeMatch || locationMatch;
      console.log(`通用搜索: 名称=${nameMatch}, 条码=${barcodeMatch}, 库位=${locationMatch} -> ${result}`);
      return result;
    }
  });
}

// 向后兼容的过滤函数
export function filterMockData(searchType, searchTerm) {
  if (!searchTerm) return [];
  
  const term = searchTerm.toLowerCase();
  
  return mockInventoryData.filter(item => {
    if (searchType === 'location') {
      return item.locationCode.toLowerCase() === term;
    } else if (searchType === 'barcode') {
      return item.barcode === searchTerm;
    }
    return false;
  });
}

// 明道云工作表字段映射示例
export const fieldMappingExample = {
  // 这是字段映射的示例，实际使用时需要根据明道云工作表的实际字段ID调整
  product_image: '控件ID_图片字段',
  product_name: '控件ID_商品名称',
  storage_condition: '控件ID_存储条件',
  supplier: '控件ID_供应商',
  barcode: '控件ID_商品条码',
  production_date: '控件ID_生产日期',
  available_stock: '控件ID_可用库存',
  locked_stock: '控件ID_锁定库存',
  total_stock: '控件ID_总库存',
  location_code: '控件ID_库位编码'
};

// 数据类型映射
export const dataTypeMapping = {
  text: 2,      // 文本类型
  number: 6,    // 数值类型
  date: 15,     // 日期类型
  attachment: 14 // 附件类型
};

// 筛选条件类型映射
export const filterTypeMapping = {
  equal: 1,          // 等于
  notEqual: 2,       // 不等于
  contains: 3,       // 包含
  notContains: 4,    // 不包含
  startsWith: 5,     // 开始于
  endsWith: 6,       // 结束于
  greaterThan: 7,    // 大于
  lessThan: 8,       // 小于
  greaterEqual: 9,   // 大于等于
  lessEqual: 10      // 小于等于
};